package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.MemberCourseDto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.entity.MemberCourse;
import com.wjc.server.entity.MemberCourseExample;
import com.wjc.server.mapper.MemberCourseMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class MemberCourseService {
    @Resource
    private MemberCourseMapper memberCourseMapper;

    @Resource
    private SnowFlake snowFlake;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        MemberCourseExample memberCourseExample = new MemberCourseExample();
        List<MemberCourse> memberCourseList= memberCourseMapper.selectByExample(memberCourseExample);
        PageInfo<MemberCourse> pageInfo=new PageInfo<>(memberCourseList);
        pageDto.setTotal(pageInfo.getTotal());

        List<MemberCourseDto> memberCourseDtosList = CopyUtil.copyList(memberCourseList,MemberCourseDto.class);
        pageDto.setList(memberCourseDtosList);

    }

    //保存
    public void save(MemberCourseDto memberCourseDto){
            Date now =new Date();

        MemberCourse memberCourse=CopyUtil.copy(memberCourseDto,MemberCourse.class);
        if (ObjectUtils.isEmpty(memberCourse.getId())){
            //新增
            memberCourse.setId(snowFlake.nextId());
            memberCourseMapper.insert(memberCourse);
        }else {
            memberCourseMapper.updateByPrimaryKey(memberCourse);
        }

    }

    public void delete(Long id){
        memberCourseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 新增
     */
    private void insert(MemberCourse memberCourse) {
        Date now = new Date();
        memberCourse.setId(snowFlake.nextId());
        memberCourse.setAt(now);
        memberCourseMapper.insert(memberCourse);
    }

    /**
     * 报名，先判断是否已报名
     * @param memberCourseDto
     */
    public MemberCourseDto enroll(MemberCourseDto memberCourseDto) {
        MemberCourse memberCourseDb = this.select(memberCourseDto.getMemberId(), memberCourseDto.getCourseId());
        if (memberCourseDb == null) {
            MemberCourse memberCourse = CopyUtil.copy(memberCourseDto, MemberCourse.class);
            this.insert(memberCourse);
            // 将数据库信息全部返回，包括id, at
            return CopyUtil.copy(memberCourse, MemberCourseDto.class);
        } else {
            // 如果已经报名，则直接返回已报名的信息
            return CopyUtil.copy(memberCourseDb, MemberCourseDto.class);
        }
    }

    /**
     * 根据memberId和courseId查询记录
     */
    public MemberCourse select(Long memberId, Long courseId) {
        MemberCourseExample example = new MemberCourseExample();
        example.createCriteria()
                .andCourseIdEqualTo(courseId)
                .andMemberIdEqualTo(memberId);
        List<MemberCourse> memberCourseList = memberCourseMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(memberCourseList)) {
            return null;
        } else {
            return memberCourseList.get(0);
        }
    }

    /**
     * 获取报名信息
     */
    public MemberCourseDto getEnroll(MemberCourseDto memberCourseDto) {
        MemberCourse memberCourse = this.select(memberCourseDto.getMemberId(), memberCourseDto.getCourseId());
        return CopyUtil.copy(memberCourse, MemberCourseDto.class);
    }

}
